﻿using FC.ERP.TMom.Api.AppHandlers.Commands;
using FC.ERP.TMom.Api.AppHandlers.Commands.EssentialDatas.FactoryManages.Lines;
using FC.ERP.TMom.Api.AppHandlers.Commands.EssentialDatas.FactoryManages.WorkShops;
using FC.ERP.TMom.Api.AppHandlers.Commands.EssentialDatas.FactoryManages.Workstations;
using FC.ERP.TMom.Api.DTOs;
using FC.ERP.TMom.Domain;
using FC.ERP.TMom.ErrorCode;
using FC.ERP.TMom.Infrastructure;
using FC.ERP.TMom.Infrastructure.Interfaces;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MiniExcelLibs;

namespace FC.ERP.TMom.Api.Controllers
{
    // ERP.Controllers/WorkOrderController.cs
    [ApiController]
    [Route("api/[controller]/[action]")]
    [Authorize]
    public class WorkOrdersController : ControllerBase
    {
        private readonly ErpDbContext _context;
        private readonly IMediator _mediator;

        public WorkOrdersController(ErpDbContext context, IMediator mediator)
        {
            _context = context;
            _mediator = mediator;
        }

        // GET: api/workorders?status=Released
        [HttpGet]
        public async Task<ActionResult<IEnumerable<WorkOrder>>> GetWorkOrders([FromQuery] string status = "Released")
        {
            // 使用EF Core从ERP数据库查询已下发的工单
            return await _context.WorkOrders
                                 .Where(wo => wo.Status == status)
                                 .ToListAsync();

        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        [AllowAnonymous]
        public Task<APIResult<SyUserDto>> loginapp(LoginQuertyCommand command)
        {
            return _mediator.Send(command);
            
        }
        /// <summary>
        /// 获取车间信息
        /// </summary>
        /// <returns>返回任务</returns>
        [HttpGet]
        public Task<APIResult<APIPaging<WorkShopDTO>>> Getwork(int pageIndex,int pageSize)
        {
            WorkShopQuertyCommand command = new WorkShopQuertyCommand();
            command.PageIndex = pageIndex;
            command.PageSize = pageSize;
            return _mediator.Send(command);
        }

        [HttpPost]
        public async Task<APIResult<int>> UpdateWork(WorkShopUpdateCommand command)
        {
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 获取产线信息
        /// </summary>
        /// <returns>返回任务</returns>
        [HttpGet]
        public Task<APIResult<APIPaging<LineDTO>>> GetLine(int pageIndex, int pageSize)
        {
            LineQuertyCommand command = new LineQuertyCommand();
            command.PageIndex = pageIndex;
            command.PageSize = pageSize;
            return _mediator.Send(command);
        }
        /// <summary>
        /// 添加车间
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<APIResult<int>> AddWork(WorkShopAddCommand command)
        {
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 添加产线信息
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<APIResult<int>> AddStation(LineAddCommand command)
        {
            return await _mediator.Send(command);
        }
        /// <summary>
        /// 获取车间信息
        /// </summary>
        /// <returns>返回任务</returns>
        [HttpGet]
        public Task<APIResult<List<LineDTO>>> GetworkLine()
        {
            WorkShopCommand command = new WorkShopCommand();
            return _mediator.Send(command);
        }

        /// <summary>
        /// 获取工位信息
        /// </summary>
        /// <returns>返回任务</returns>
        [HttpGet]
        [AllowAnonymous]
        public Task<APIResult<APIPaging<WorkstationDTO>>> GetWorkstation(int pageIndex, int pageSize)
        {
            WorkstationQuertyCommand command = new WorkstationQuertyCommand();
            command.PageIndex = pageIndex;
            command.PageSize = pageSize;
            return _mediator.Send(command);
        }
        /// <summary>
        /// 添加工位信息
        /// </summary>
        /// <returns>返回任务</returns>
        [HttpGet]
        public async Task<APIResult<int>> AddWorkstation(WorkstationAddCommand command)
        {
            return await _mediator.Send(command);
        }

        [HttpGet]
        public Task<APIResult<List<LineDTO>>> Getlines()
        {
            LineCommand command = new LineCommand();
            return _mediator.Send(command);
        }

        [HttpGet]
        public Task<APIResult<List<ProcessOperationDTO>>> GetProcess()
        {
            ProcessOperationQuertyCommand command = new ProcessOperationQuertyCommand();
            return _mediator.Send(command);
        }
    }
}
